package com.woniu113.junk.user.consumer;

import com.woniu113.junk.commons.util.RedisUtil;
import com.woniu113.junk.user.util.RedisKeyUtil;
import com.woniu113.junk.user.util.SMSUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@RabbitListener(queues = "register_phone_queue")
@Component
public class RegisterPhoneQueueConsumer {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private SMSUtil smsUtil;
    @RabbitHandler
    public void sendCode(String mobile)throws Exception{
        String code = smsUtil.sendCode(mobile);
        //将验证码存入redis
        redisUtil.set(RedisKeyUtil.REGISTER_CODE_kEY(mobile),code,60*5);
        //发送完验证码以后想redis保存一个数据有效期1min
        redisUtil.set(RedisKeyUtil.REGISTER_CODE_SENT(mobile),"",60);
        //发送验证码之后记录发送次数+1
        if (!redisUtil.exists(RedisKeyUtil.SEND_NUMBER(mobile))){
            redisUtil.set(RedisKeyUtil.SEND_NUMBER(mobile),"1",60*60);
        }else {
            redisUtil.increment(RedisKeyUtil.SEND_NUMBER(mobile));
        }
    }
}
